package com.leo;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBar;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

/**
 * @ClassName ButtonBarDemo
 * @Description
 * @Author wangss
 * @date 2020.05.22 00:44
 * @Version 1.0
 */
public class ButtonBarDemo extends Application implements EventHandler<ActionEvent> {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane anchorPane = new AnchorPane();
        anchorPane.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(anchorPane);

        ButtonBar buttonBar = new ButtonBar();
        Button button1 = new Button("button1");
        Button button2 = new Button("button2");
        Button button3 = new Button("button3");

        ButtonBar.setButtonData(button1, ButtonBar.ButtonData.APPLY);
        ButtonBar.setButtonData(button2, ButtonBar.ButtonData.FINISH);
        ButtonBar.setButtonData(button3, ButtonBar.ButtonData.NO);

        buttonBar.getButtons().addAll(button1, button2, button3);
        buttonBar.setButtonOrder(ButtonBar.BUTTON_ORDER_WINDOWS);

        anchorPane.getChildren().addAll(buttonBar);

        primaryStage.setScene(scene);
        primaryStage.setTitle("菜单");
        primaryStage.setWidth(625d);
        primaryStage.setHeight(500d);
        primaryStage.setResizable(false);
        primaryStage.show();

        button1.setOnAction(this::handle);
        button2.setOnAction(this::handle);
        button3.setOnAction(this::handle);

    }

    @Override
    public void handle(ActionEvent event) {
        Button button = (Button) event.getSource();
        if (ButtonBar.getButtonData(button).equals(ButtonBar.ButtonData.APPLY)) {
            System.out.println("button1");
        } else if (ButtonBar.getButtonData(button).equals(ButtonBar.ButtonData.FINISH)) {
            System.out.println("button2");
        } else {
            System.out.println("button3");
        }
    }
}
